package architecture.view;

import javax.swing.*;

import db.*;

import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;

public class SettingsDialog extends JDialog implements ActionListener {
	final int size=3;
	JTextField txt[];
	JLabel lab[];
	String labels[] = {"Korisnicko ime:", "Lozinka:", "E-Mail:"};
	JButton ok, cancel;
	MainForm main;
	String user;

	final int startx = 10;
	final int starty = 10;
	final int delta = 15;
	final int width = 200;
	final int height = 20;

	public SettingsDialog(MainForm main)
	{
		this.main = main;
		JPanel mainPanel = new JPanel(null);
		txt = new JTextField[3];
		lab = new JLabel[3];
		for (int i = 0; i < txt.length; i++) {
			mainPanel.add(txt[i] = new JTextField(20));
			mainPanel.add(lab[i] = new JLabel(labels[i]));
			lab[i].setBounds(startx, starty + 2*i * (height + delta), width,
					height);
		
			txt[i].setBounds(startx, starty + (2*i+1) * (height + delta), width,
					height);
		}
		txt[0].setEditable(false);

		JPanel buttonPanel = new JPanel(new FlowLayout());
		buttonPanel.add(ok = new JButton("OK"));
		buttonPanel.add(cancel = new JButton("Cancel"));
		ok.addActionListener(this);
		cancel.addActionListener(this);

		add(mainPanel);
		add(buttonPanel, BorderLayout.SOUTH);

		ebDBDataSet ds = new ebDBDataSet();
		ds.setTableName("KLIJENTI_KORISNICI");
		ds.Prepare();
		ds.Open();

		user = main.getUser().userName;

		ResultSet rs = ds.getResultSet();

		try {
			while (rs.next()) {
				if (rs.getString("KL_IME").compareTo(user) == 0) {
					txt[0].setText(rs.getString("KL_IME"));
					txt[1].setText(rs.getString("KL_LOZINKA"));
					txt[2].setText(rs.getString("KL_E_MAIL"));
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setBounds(300, 300, 250, 350);
		setTitle("User settings");
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		JButton source = (JButton) e.getSource();
		if (source == ok) {
			ebDBDataSet ds = new ebDBDataSet();
			ds.setTableName("KLIJENTI_KORISNICI");
			ds.Prepare();
			ds.Open();

			try {
				ResultSet rs = ds.getResultSet();

				while (rs.next()) {
					if (rs.getString("KL_IME").compareTo(user) == 0) {
						LinkedHashMap<String, Object> tmp = new LinkedHashMap();

						tmp.clear();
						tmp.put("PRA_DR_SIFRA2", rs.getString("PRA_DR_SIFRA2"));
						tmp.put("PRA_PL_SIFRA", rs.getString("PRA_PL_SIFRA"));
						tmp.put("KL_ID", rs.getString("KL_ID"));
						tmp.put("PRA_DR_SIFRA", rs.getString("PRA_DR_SIFRA"));
						tmp.put("PL_SIFRA", rs.getString("PL_SIFRA"));
						tmp.put("SIFRA_GRUPE", rs.getString("SIFRA_GRUPE"));
						tmp.put("DR_SIFRA", rs.getString("DR_SIFRA"));
						tmp.put("NM_SIFRA", rs.getString("NM_SIFRA"));
						tmp.put("KL_IME", rs.getString("KL_IME"));
						tmp.put("KL_LOZINKA", txt[1].getText());
						tmp.put("KL_E_MAIL", txt[2].getText());

						ds.update(tmp);
						
						main.getUser().email = txt[1].getText();						
						main.getUser().email = txt[2].getText();
						
						this.setVisible(false);
					}
				}
			} catch (SQLException exp) {
				// TODO Auto-generated catch block
				exp.printStackTrace();
			}

			//ds.update(updateValues)
		} else if (source == cancel) {
			this.setVisible(false);
		}
	}
}
